<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Blog
 *
 * A website, usually maintained with regular entries of commentary
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Blog extends User_Controller {
	function Blog( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Blog' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the blog  model
		$this->load->model( 'blog_model' );
		$this->load->module_model( 'article', 'website_model' );
		
		// Load the blog module
		$this->lang->module_load( 'blog', 'blog' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'blog' ), 'blog/user/blog' );
				
		log_message( 'debug', 'conlabz CMS : Blog class loaded' );
	}	
	
	/**
	 * View Blog
	 *
	 * @access public
	 */
	function index( ) {
		// Get blogs
		$data['blog']   = $this->blog_model->getBlogs( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'blog_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_blog";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'blog';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
		
	/**
	 * Get Blog Details
	 *
	 * Load blog detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_blog_details( ) {
		$data['id']    	     = $this->input->post( 'id'          );
		$data['name'] 	     = $this->input->post( 'name'        );
		$data['active']      = $this->input->post( 'active'      );
		$data['teaser']      = $this->input->post( 'teaser'      );
		$data['description'] = $this->input->post( 'description' );
	
		return $data;
	}
	
	/**
	 * Display Blog Form
	 *
	 * @access public
	 * @param integer $id Blog ID
	 */
	function form( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']          = "ID";
		$fields['name']        = $this->lang->line( 'blog_title'           );
		$fields['active']      = $this->lang->line( 'blog_comments_active' );
		$fields['teaser']      = $this->lang->line( 'blog_teaser'          );
		$fields['description'] = $this->lang->line( 'blog_description'     );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['name']        = "trim|required";
			$rules['teaser']      = "trim|required";
			$rules['description'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['name']        = "trim|required";
			$rules['teser']       = "trim|required";
			$rules['description'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$blog = $this->blog_model->getBlogs( array( 'blog.id' => $id ) );
			$blog = $blog->row_array( );
			$this->validation->set_default_value( 'blog', $blog['id'] );
			$this->validation->set_default_value( $blog );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_blog_category' ) );
			$this->validation->set_default_value( 'active', '1' );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => array( 'CustomConfigurationsPath' => DEVPATH.'/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js' )
    		);
			
			$this->validation->output_errors( );
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_blog' ) : $this->lang->line( 'edit_blog' ) );
			$this->bep_site->set_crumb( $data['header'], 'blog/user/blog/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_blog";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'blog';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$blog = $this->_get_blog_details( );
				$blog['timestamp'] = date( 'Y-m-d H:i:s' );
			
				$this->db->trans_start( );
				$blog['blog_id'] = $this->blog_model->insert( $blog );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $blog['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				redirect( 'blog/user/blog' );
			} else {
				// SAVE
				$blog = $this->_get_blog_details( );
				$blog['timestamp'] = date( 'Y-m-d H:i:s' );
				
				$this->db->trans_start( );
				$this->blog_model->update( $blog, $blog['id'], 'blog' );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $blog['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect( 'blog/user/blog' );
			}
		}
	} // form( );
	
	/**
	 * Delete
	 *
	 * Delete the selected blog from the system
	 *
	 * @access public
	 */
	function delete( ) {
		$this->load->model( 'blog_model' );	
		$entry_id = $this->blog_model->_getEntryID( );
		$this->blog_model->delete_entry( $entry_id );
		
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'blog/user/blog', 'location' );
		}

		foreach( $selected as $blog ) {
			$this->db->delete( 'blog', array( 'id' => $blog ) );
		}

		flashMsg( 'success', $this->lang->line( 'blog_deleted' ) );
		redirect( 'blog/user/blog', 'location' );
	}
	
	/************************************************* ENTRY SECTION *************************************************/
	
	/**
	 * View blog entries
	 * 
	 * @access public
	 */
	function show_entries( $blog_id ) {
		// Get entries
		$data['entry']  = $this->blog_model->getEntries( $blog_id );
		$data['blog_id'] = $blog_id;
		// Display Page
		$data['header'] = $this->lang->line( 'entries_info' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_entries";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'blog';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
	
	/**
	 * Get blog entry details
	 * 
	 * @access private
	 * @return array
	 */
	function _get_entry_details( ) {
		$data['pic'] 	   = $this->input->post( 'pic'       );
		$data['body'] 	   = $this->input->post( 'body'      );
		$data['title']     = $this->input->post( 'title'     );
		$data['active']    = $this->input->post( 'active'    );
		$data['author']    = $this->input->post( 'author'    );
		$data['teaser']    = $this->input->post( 'teaser'    );
		$data['picture']   = $this->input->post( 'picture'   );
		$data['entry_id']  = $this->input->post( 'entry_id'  );
		$data['timestamp'] = $this->input->post( 'timestamp' );

		return $data;
	}
	
	/**
	 * Display Blog Entry Form
	 *
	 * @access public
	 * @param integer $entry_id Blog Entry ID
	 */
	function form_entries( $blog_id, $entry_id = NULL ) {
		// VALIDATION FIELDS
		$fields['entry_id'] = "ID";
		$fields['pic']      = $this->lang->line( 'blog_entry_pic'    );
		$fields['body']     = $this->lang->line( 'blog_entry_body'   );
		$fields['title']    = $this->lang->line( 'blog_entry_title'  );
		$fields['active']   = $this->lang->line( 'blog_entry_active' );
		$fields['teaser']   = $this->lang->line( 'blog_entry_teaser' );
		$fields['picture']  = $this->lang->line( 'blog_entry_pic'    );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $entry_id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['body']   = "trim|required";
			$rules['title']  = "trim|required";
			$rules['teaser'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['body']   = "trim|required";
			$rules['title']  = "trim|required";
			$rules['teaser'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $entry_id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			//@todo: Specify blog_id
			$entry = $this->blog_model->getEntries( $blog_id, array( 'blog_entry.entry_id' => $entry_id ) );
			$entry = $entry->row_array( );
			$this->validation->set_default_value( 'entry', $entry['entry_id'] );
			$this->validation->set_default_value( $entry );
		} elseif( is_null( $entry_id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_blog_category' ) );
			$this->validation->set_default_value( 'active', '1' );
			$this->validation->set_default_value( 'announce', '1' );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct Picture dropdown
			$data['pic'] = $this->blog_model->buildPictureDropdown( );
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'body',
              	'id'          => 'body',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => array( 'CustomConfigurationsPath' => DEVPATH.'/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js' )
    		);
			$data['blog_id'] = $blog_id;
			$this->validation->output_errors( );
			$data['header'] = (is_null( $entry_id ) ? $this->lang->line( 'create_blog_entry' ) : $this->lang->line( 'edit_blog_entry' ) );
			$this->bep_site->set_crumb( $data['header'], 'blog/user/blog/form_entries/'.$entry_id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_entries";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'blog';
			$this->bep_assets->load_asset('load.js'); // NVD 15.09.09
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $entry_id ) ) {
				// CREATE
				// Fetch form values
				$entry = $this->_get_entry_details( );
				$entry['timestamp'] = date( 'Y-m-d H:i:s' );
				$entry['blog_id'] = $blog_id;
				$this->db->trans_start( );
				$entry['entry_id'] = $this->blog_model->insert_entry( $entry );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $entry['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				//@todo: specify $blog_id
				redirect( 'blog/user/blog/show_entries/'.$blog_id );
			} else {
				// SAVE
				$entry = $this->_get_entry_details( );
				$entry['timestamp'] = date( 'Y-m-d H:i:s' );
				$entry['blog_id'] = $blog_id;				
				$this->db->trans_start( );
				$this->blog_model->update_entries( $entry, $entry['entry_id'], 'blog_entry' );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $entry['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				//@todo: specify $blog_id
				redirect( 'blog/user/blog/show_entries/'.$blog_id );
			}
		}
	} // form_entries( );
	
	/**
	 * Delete
	 *
	 * Delete the selected entry from the system
	 *
	 * @access public
	 */
	function delete_entry( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'blog/user/blog', 'location' );
		}

		foreach( $selected as $entry ) {
			$this->db->delete( 'blog_entry', array( 'entry_id' => $entry ) );
		}

		flashMsg( 'success', $this->lang->line( 'blog_entry_deleted' ) );
		redirect( 'blog/user/blog', 'location' );
	}
}
/* End of file blog.php */
/* Location: ./modules/blog/controllers/user/blog.php */